package com.slipper.codeless.controller;

import com.slipper.groovy.service.GroovyService;
import com.slipper.groovy.vo.Message;
import com.slipper.util.Tool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping
public class QrcodeController {
    @GetMapping(value = "/qrcode/{scriptId}/{data}")
    public String qrcodeValidate(
            @PathVariable("scriptId") String scriptId,
            @PathVariable("data") String data) {
        if (Tool.isBlank(scriptId) || Tool.isBlank(data)) {
            return "异常数据，无效二维码！";
        }

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("data", data);
        Object result = GroovyService.runById(scriptId, dataMap);
        if (result instanceof Message message) {
            return message.getMessage();
        } else {
            return "异常数据，无效二维码！";
        }
    }
}
